package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SpuDetailService detailService;

    @Autowired
    private SkuService skuService;

    //分页查询商品spu
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id
    ){
        PageDTO<SpuDTO> pageDTO = spuService.querySpuByPage(page,rows,saleable,categoryId,brandId,id);
        return ResponseEntity.ok(pageDTO);
    }


    //新增商品
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //修改商品上下架
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id") Long spuId,@RequestParam("saleable") Boolean saleable){
        spuService.updateSpuSaleable(spuId,saleable);
        //更新成功，返回204
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //编辑下架的商品前 回显数据
    //根据商品id查询商品的spu、sku、spuDetail等信息
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
        SpuDTO spuDTO = spuService.queryGoodsById(id);
        return ResponseEntity.ok(spuDTO);
    }

    //商品修改????????????????????????????
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        spuService.updateGoods(spuDTO);
        return ResponseEntity.noContent().build();
    }

    //根据id查询商品详情SpuDetail
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id){
        SpuDetailDTO spuDetailDTO = detailService.querySpuDetailById(id);
        return ResponseEntity.ok(spuDetailDTO);
    }

    //根据SpuId查询SKU集合
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long spuId){
        List<SkuDTO> skuDTOList = skuService.querySkuBySpuId(spuId);
        return ResponseEntity.ok(skuDTOList);
    }

    //根据主键id集合查询SKU集合
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") Collection<Long> ids){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.listByIds(ids)));
    }

    //根据spuId查询spu的所有规格参数键值对
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(@RequestParam(value = "id") Long spuId,
                                                               @RequestParam(value = "searching",required = false) Boolean searching){
        List<SpecParamDTO> specParamDTOList = spuService.querySpecsValues(spuId,searching);
        return ResponseEntity.ok(specParamDTOList);
    }

    //根据Id查询商品Spu
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }

    /**
     * 扣减库存
     * @param skuMap sku信息，key：skuId，value：扣减数量
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long,Integer> skuMap){
        skuService.deductStock(skuMap);
        return ResponseEntity.noContent().build();
    }
}